home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / program / tsetguid.zip / TEA / SET / SAMPLE / DYNATREE.JAV < prev    next >
Text File  |  1997-02-27  |  4KB  |  122 lines

  1. /*
  2.  * Copyright (c) 1996-1997, InetSoft Technology Corp, All Rights Reserved.
  3.  *
  4.  * The software and information contained herein are copyrighted and 
  5.  * proprietary to InetSoft Technology Corp. This software is furnished 
  6.  * pursuant to a written license agreement and may be used, copied, 
  7.  * transmitted, and stored only in accordance with the terms of such 
  8.  * license and with the inclusion of the above copyright notice. Please 
  9.  * refer to the file "COPYRIGHT" for further copyright and licensing 
  10.  * information. This software and information or any other copies 
  11.  * thereof may not be provided or otherwise made available to any 
  12.  * other person. 
  13.  */
  14. package tea.set.sample;
  15.  
  16. import tea.set.*;
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import java.io.*;
  20.  
  21. /**
  22.  * DynaTree is an example program to demostrate Forest widget. It 
  23.  * takes a path, and generates a directory tree. However the directory
  24.  * tree is not read in at the beginning. When an user clicks on a
  25.  * directory node, the nodes under the directory is read in and
  26.  * populated to the tree.
  27.  *
  28.  * @see Forest
  29.  * @version 1.3, 01/31/97
  30.  * @author InetSoft Technology Corp
  31.  */
  32. public class DynaTree extends Frame {
  33.    /**
  34.     * Create a directory tree from the root path.
  35.     */
  36.    public DynaTree(String rootPath) {
  37.       setLayout(new BorderLayout());
  38.     
  39.       // create Forest and set appropriate options
  40.       forest = new Forest(Forest.LINE);
  41.       // use the same separator as the file system for separating nodes
  42.       forest.setSeparator(File.separatorChar);
  43.       // open/close folder only if mouse click is in icon
  44.       forest.setIconOnly(true);
  45.       
  46.       // never close root folder
  47.       forest.forceOpen(rootPath, true);
  48.  
  49.       // populate the first level nodes
  50.       populateTree(rootPath);
  51.       
  52.       // put forest inside a Scroller to ensure all parts of forest
  53.       // can be seen
  54.       add(scroller = new Scroller(forest, true, 200, 200), "Center");
  55.       Button b = new Button("Cancel");
  56.       add(b, "North");
  57.       
  58.       b.addActionListener(new ActionListener() {
  59.      public void actionPerformed(ActionEvent e) {
  60.         dispose();
  61.         System.exit(0);
  62.      }
  63.       });
  64.       
  65.       forest.addItemListener(new ItemListener() {
  66.      public void itemStateChanged(ItemEvent e) {
  67.         Node node = (Node) e.getItem();
  68.         String path = node.getPath();
  69.         File root = new File(path);
  70.         if(root.isDirectory() && node.isLeaf()) {
  71.            populateTree(path);
  72.            scroller.notifyUpdate();
  73.         }
  74.      }
  75.       });
  76.    }
  77.  
  78.    // populate the tree nodes from the directory information.
  79.    public void populateTree(String rootPath) {
  80.       // add current dir/file to the tree
  81.       forest.findNode(rootPath, true);
  82.       
  83.       try {
  84.      File root = new File(rootPath);
  85.      
  86.      if(root.isDirectory()) {
  87.         String[] files = root.list();
  88.         
  89.         if(files != null) {
  90.            for(int i = 0; i < files.length; i++) {
  91.           String path = rootPath + File.separator + files[i];
  92.           forest.findNode(path, true);
  93.            }
  94.         }
  95.      }
  96.       }
  97.       catch(Exception e) {
  98.      e.printStackTrace();
  99.       }
  100.       
  101.       forest.doLayout();
  102.       forest.repaint();
  103.    }
  104.    
  105.    /**
  106.     * Pass a directory path as command line parameter. On Win32 platform,
  107.     * beware that the backslash needs to be escaped.
  108.     * For a directory tree from current directory, type:
  109.     * java tea.set.sample.FileTree .
  110.     */
  111.    public static void main(String argv[]) {
  112.       DynaTree tree = new DynaTree(argv[0]);
  113.       tree.pack();
  114.       tree.forest.setImage(tree.forest.getImage(Forest.FOLDER_CLOSE),
  115.                Forest.FOLDER_LEAF);
  116.       tree.show();
  117.    }
  118.    
  119.    private Forest forest;
  120.    private Scroller scroller;
  121. }
  122.